package de.java.shortys.myhome.fuelmonitor.controller;

import static de.java.shortys.myhome.fuelmonitor.spring.BeanConstants.APPLICATION_CONTEXT_PATH;
import static de.java.shortys.myhome.fuelmonitor.spring.BeanConstants.FUELMONITOR_SERVICE_BEAN;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import de.java.shortys.myhome.api.controller.AbstractSwingApplicationController;
import de.java.shortys.myhome.api.controller.AbstractSwingModuleController;
import de.java.shortys.myhome.api.controller.IController;
import de.java.shortys.myhome.api.view.SwingModuleView;
import de.java.shortys.myhome.fuelmonitor.model.IFuelFilling;
import de.java.shortys.myhome.fuelmonitor.model.IFuelMonitorService;
import de.java.shortys.myhome.fuelmonitor.view.ExtendedViewAverageConsumption;

public class FuelMonitorController extends AbstractSwingModuleController {

	public static final String NAME = "FuelMonitor";
	public static final String ID = "FuelMonitorModule";
	public static final String VERSION = "0.0.1";

	private static ApplicationContext context;

	private IFuelMonitorService fuelMonitorService;

	public static ApplicationContext getContext() {
		if (context == null) {
			context = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_PATH);
		}
		return context;
	}

	public IFuelFilling getFuelFillingByKey(String key) {
		for (IFuelFilling fuelFilling : fuelMonitorService.getFuelMonitor().read()) {
			if (fuelFilling.getKey().equals(key)) {
				return fuelFilling;
			}
		}

		return null;
	}

	public IFuelMonitorService getFuelMonitorService() {
		if (fuelMonitorService == null) {
			fuelMonitorService = (IFuelMonitorService) getContext().getBean(FUELMONITOR_SERVICE_BEAN);
		}
		return fuelMonitorService;
	}

	public FuelMonitorController(final AbstractSwingApplicationController applicationController) {
		super(applicationController);
	}

	@Override
	public boolean allowActivate() {
		return true;
	}

	@Override
	public boolean allowDeactivate() {
		return false;
	}

	public void createFuelFilling(IFuelFilling fuelFilling) {
		fuelMonitorService.create(fuelFilling);
	}

	public void updateFuelFilling(IFuelFilling fuelFilling) {
		fuelMonitorService.update(fuelFilling);
	}

	public void deleteFuelFilling(IFuelFilling fuelFilling) {
		fuelMonitorService.delete(fuelFilling);
	}

	@Override
	public String getIcon() {
		return null;
	}

	@Override
	public String getTitle() {
		return NAME;
	}

	@Override
	public void initialize() {
		SubModuleControllerOverview subMobuleViewOverview = new SubModuleControllerOverview(this, this);
		getFuelMonitorService().getFuelMonitor().addObserver(subMobuleViewOverview.getView());
		add(subMobuleViewOverview);
		add(new SubModuleControllerFillingAdd(this, this));
		ExtendedViewAverageConsumption extendedView = new ExtendedViewAverageConsumption();
		getFuelMonitorService().getFuelMonitor().addObserver(extendedView);
		SwingModuleView moduleView = (SwingModuleView) getView();
		moduleView.setExtendedView(extendedView);
	}

	@Override
	public void onActivate() {
		// Empty
	}

	@Override
	public void onDeactivate() {
		for (IController controller : getChildren()) {
			controller.deactivate();
		}
	}

	@Override
	public void onInitialize() {
		// Empty
	}

	@Override
	public String getId() {
		return ID;
	}

}
